﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using MicropleSoft.WmsServiceLibrary.BusinessInterfaces;
using MicropleSoft.WmsServiceLibrary.Common;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

namespace MicropleSoft.WmsServiceLibrary.Unity
{
    public static class ContainerFactory
    {
        private static  UnityContainer _unityContainerInstance = null;

        static ContainerFactory()
        {
            ContainerConfigurationFromFile("Unity.config");
        }
         
        public static T GetObjectFromUnityContainer<T>()
        {
            return _unityContainerInstance.Resolve<T>();
        }

        public static UnityContainer GetUnityContainer()
        {
            return _unityContainerInstance;
        }

        private static void ContainerConfigurationFromFile(string configFile)
        {

            string file = ApplicationPath.GetApplicationPath()+ configFile;
            //根据文件名获取指定config文件
            var fileMap = new ExeConfigurationFileMap { ExeConfigFilename = file };
            //从config文件中读取配置信息
            Configuration configuration =
                ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
            var unitySection = (UnityConfigurationSection)configuration.GetSection("unity");

            (_unityContainerInstance = new UnityContainer()).LoadConfiguration(unitySection, "Default");

        }
    }
}
